/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayGameForm.java
 *
 * Created on Apr 2, 2012, 9:46:54 AM
 */
package View;

import Constructor.Common;
import DAO.DAO_Answer;
import DAO.DAO_Question;
import DAO.DAO_UserQuest;
import Model.Model_Answer;
import Model.Model_Question;
import Model.Model_User_Quest;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import org.jdesktop.beansbinding.Converter;

/**
 *
 * @author Admin
 */
public class PlayGameForm extends javax.swing.JFrame {

    /**
     * Creates new form PlayGameForm
     */
    public int questNo = 1;

    public PlayGameForm() {
        initComponents();
        loadForm();
    }

    private void loadForm() {
        setTitle("Play Game");
        this.setSize(480, 250);
        ImageIcon icon = new ImageIcon(getClass().getResource("/Images/logo.png"));
        Image image = icon.getImage();
        setIconImage(image);
        Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((ScreenSize.width - 480) / 2, (ScreenSize.height - 250) / 2);
        //setLocation(null);
        setResizable(false);

        setVisible(true);

        //load_randomID();
        load_Question();
    }

    public void load_randomID() {
        //txtTotalQuest.setText("" + randomQuestID());
    }

    //get random Quest_ID from database
    public int randomQuestID() {
        DAO_Question daoQuest = new DAO_Question();
        //random the ID get from database
        Random generator = new Random();
        int randomID = generator.nextInt(Integer.parseInt(daoQuest.get_TotalQuestion()));
        //check randomID which user already played or not
        DAO_UserQuest daoUQ = new DAO_UserQuest();
        ArrayList<Model_User_Quest> listUser_Quest = new ArrayList<Model_User_Quest>();
        boolean check;
        while (true) {
            listUser_Quest = daoUQ.get_AllUser_QuestbyUserID(Common._UserID);
            check = true;
            for (Model_User_Quest user_quest : listUser_Quest) {
                if (randomID == Integer.parseInt(user_quest.getQuestion_id())) {
                    randomID = generator.nextInt(Integer.parseInt(daoQuest.get_TotalQuestion()));
                    check = false;
                    break;
                }
            }
            if (check == true) {
                break;
            }
        }

        return randomID;
    }
    
    public void addUser_Quest(int user_id, int quest_id){
        DAO_UserQuest daoUQ = new DAO_UserQuest();
        Model_User_Quest user_quest = new Model_User_Quest();
        user_quest.setUser_id(""+user_id);
        user_quest.setQuestion_id(""+quest_id);
        daoUQ.insertUser_Quest(user_quest);
    }

    //load the question to form 
    public void load_Question() {
        //load question from database
        DAO_Question daoQuest = new DAO_Question();
        DAO_Answer daoAns = new DAO_Answer();
        
        Common._questID = randomQuestID();
        Model_Question quest = daoQuest.get_OneQuestionbyID_Status(Common._questID);
        //update question status to 1
        if (daoQuest.updateQuestStatusbyID(Common._questID, 1) == false) {
            JOptionPane.showMessageDialog(null, "Error !");
        }
        //insert to user_id and question_id to user_quest to notice this user had played this answer already
        addUser_Quest(Common._UserID, Common._questID);
        
        //get 4 answer of the question
        ArrayList<Model_Answer> listAns = daoAns.get_AnswerbyQuestionID(Integer.parseInt(quest.getId()));
        lblQuestNo.setText("Question " + questNo + " :");
        lbl_Quest.setText(quest.getContent());
        //load answer for the question
        int i = 0;
        for (Model_Answer ans : listAns) {
            if (i == 0) {
                rdo_Ans1.setText(ans.getContent());
                rdo_Ans1.setActionCommand(ans.getCorrect());
                grp_RDO.add(rdo_Ans1);
                i++;
            } else if (i == 1) {
                rdo_Ans2.setText(ans.getContent());
                rdo_Ans2.setActionCommand(ans.getCorrect());
                grp_RDO.add(rdo_Ans2);
                i++;
            } else if (i == 2) {
                rdo_Ans3.setText(ans.getContent());
                rdo_Ans3.setActionCommand(ans.getCorrect());
                grp_RDO.add(rdo_Ans3);
                i++;
            } else if (i == 3) {
                rdo_Ans4.setText(ans.getContent());
                rdo_Ans4.setActionCommand(ans.getCorrect());
                grp_RDO.add(rdo_Ans4);
                i++;
            }
        }
        questNo++;
        //lbl_Quest.setText(quest.getContent());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grp_RDO = new javax.swing.ButtonGroup();
        lbl_Quest = new javax.swing.JLabel();
        lblQuestNo = new javax.swing.JLabel();
        rdo_Ans1 = new javax.swing.JRadioButton();
        rdo_Ans2 = new javax.swing.JRadioButton();
        rdo_Ans3 = new javax.swing.JRadioButton();
        rdo_Ans4 = new javax.swing.JRadioButton();
        btn_Submit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblQuestNo.setText("Question");

        btn_Submit.setText("Submit");
        btn_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SubmitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rdo_Ans1)
                                    .addComponent(rdo_Ans2))
                                .addGap(85, 85, 85)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rdo_Ans4)
                                    .addComponent(rdo_Ans3)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblQuestNo, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14)
                                .addComponent(lbl_Quest, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(14, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_Submit)
                        .addGap(62, 62, 62))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(70, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblQuestNo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_Quest, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdo_Ans3)
                    .addComponent(rdo_Ans1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(rdo_Ans2)
                    .addComponent(rdo_Ans4))
                .addGap(10, 10, 10)
                .addComponent(btn_Submit)
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //check answer that true or false
    public boolean checkAnswer(JRadioButton temp) {
        if (Integer.parseInt(temp.getActionCommand()) == 0) {
            return false;
        } else {
            return true;
        }
    }

    //view the result that user answer worng or right
    public void load_Result(boolean temp) {
        if (temp == true) {
            String correct = "Congratulation ! You have pass this question.";
            JOptionPane.showMessageDialog(null, correct);
            load_Question();
            grp_RDO.clearSelection();
        } else {
            String wrong = "Your answer is wrong !";
            JOptionPane.showMessageDialog(null, wrong);
            DAO_Question daoQuest = new DAO_Question();
            //upadte question status = 0 when user answer wrong
            if (daoQuest.updateQuestStatusbyID(Common._questID, 0) == false) {
                JOptionPane.showMessageDialog(null, "Error !");
            }
            grp_RDO.clearSelection();
        }
    }

    private void btn_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SubmitActionPerformed
        String missAns = "You have missed the answer !";
        if (rdo_Ans1.isSelected()) {
            load_Result(checkAnswer(rdo_Ans1));
        } else if (rdo_Ans2.isSelected()) {
            load_Result(checkAnswer(rdo_Ans2));
        } else if (rdo_Ans3.isSelected()) {
            load_Result(checkAnswer(rdo_Ans3));
        } else if (rdo_Ans4.isSelected()) {
            load_Result(checkAnswer(rdo_Ans4));
        } else {
            JOptionPane.showMessageDialog(null, missAns);
        }
    }//GEN-LAST:event_btn_SubmitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PlayGameForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Submit;
    private javax.swing.ButtonGroup grp_RDO;
    private javax.swing.JLabel lblQuestNo;
    private javax.swing.JLabel lbl_Quest;
    private javax.swing.JRadioButton rdo_Ans1;
    private javax.swing.JRadioButton rdo_Ans2;
    private javax.swing.JRadioButton rdo_Ans3;
    private javax.swing.JRadioButton rdo_Ans4;
    // End of variables declaration//GEN-END:variables
}
